import type { GlobEnvConfig } from '/#/config'
import { warn } from '@/utils/log'
import { getConfigFileName } from '../../build/getConfigFileName'

// 获取环境配置文件内容
export function getAppEnvConfig() {
  // const ENV_NAME = getConfigFileName(import.meta.env)
  // let ENV = (import.meta.env.DEV
  //   ? // 获取全局配置（打包时将独立提取配置）
  //     (import.meta.env as unknown as GlobEnvConfig)
  //   : window[ENV_NAME as any]) as unknown as GlobEnvConfig

  let ENV = import.meta.env as unknown as GlobEnvConfig
  ENV = ENV || {}
  const {
    VITE_GLOB_APP_TITLE, // 应用标题
    VITE_GLOB_API_URL, //
    VITE_GLOB_APP_SHORT_NAME,
    VITE_GLOB_API_URL_PREFIX,
    VITE_GLOB_UPLOAD_URL,
    VITE_GLOB_PROD_MOCK,
    VITE_GLOB_IMG_URL,
    VITE_GLOB_PRIVIEW_FILE_URL,
    VITE_GLOB_PRIVIEW_FILE_TYPE,
    VITE_GLOB_CAPTURE_URL,
    VITE_GLOB_ONLINE_EDIT_URL
  } = ENV

  // VITE_GLOB_APP_SHORT_NAME 应用名称缩写必须是英文构成的，如果不是，抛出异常
  if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) {
    warn(`VITE_GLOB_APP_SHORT_NAME 变量只能是字符/下划线，请在环境变量中修改并重新运行`)
  }

  return {
    VITE_GLOB_APP_TITLE,
    VITE_GLOB_API_URL,
    VITE_GLOB_APP_SHORT_NAME,
    VITE_GLOB_API_URL_PREFIX,
    VITE_GLOB_UPLOAD_URL,
    VITE_GLOB_PROD_MOCK,
    VITE_GLOB_IMG_URL,
    VITE_GLOB_PRIVIEW_FILE_URL,
    VITE_GLOB_PRIVIEW_FILE_TYPE,
    VITE_GLOB_CAPTURE_URL,
    VITE_GLOB_ONLINE_EDIT_URL
  }
}
